﻿using System;
using System.Security.Cryptography;
using System.Text;

namespace AutoLoL.Logic.Utils
{
    public class Encryption
    {
        public static string Protect(string password)
        {
            if (string.IsNullOrEmpty(password))
                return string.Empty;

            byte[] bytes = Encoding.Unicode.GetBytes(password);
            byte[] protectedPassword = ProtectedData.Protect(bytes, null, DataProtectionScope.CurrentUser);
            return Convert.ToBase64String(protectedPassword);
        }

        public static string Unprotect(string protectedPassword)
        {
            if (string.IsNullOrEmpty(protectedPassword))
                return string.Empty;

            byte[] bytes = Convert.FromBase64String(protectedPassword);
            byte[] password = ProtectedData.Unprotect(bytes, null, DataProtectionScope.CurrentUser);
            return Encoding.Unicode.GetString(password);
        }

        public static string GetMD5(string original)
        {
            MD5 md5 = new MD5CryptoServiceProvider();
            Byte[] originalBytes = ASCIIEncoding.Default.GetBytes(original);
            Byte[] encodedBytes = md5.ComputeHash(originalBytes);
            return BitConverter.ToString(encodedBytes);
        }
    }
}